package com.kovalik;

import android.app.ListActivity;
import android.content.ContentUris;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Contacts.People;
import android.provider.Contacts.Phones;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.database.Cursor;

@SuppressWarnings("deprecation")
public class MultipleItemsList extends ListActivity {
	 
	    private MySimpleCursorAdapter myAdapter;
	    
	  
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        Cursor cursor = getContentResolver().query(People.CONTENT_URI, null, null, null, People.NAME);
	        startManagingCursor(cursor);
	       
	        String[] columns = new String[] {People.NAME, People.NUMBER};
	        int[] names = new int[] {R.id.contact_name, R.id.phone_number};
	        
	        myAdapter = new MySimpleCursorAdapter(this, R.layout.main, cursor, columns, names);
	    
	        setListAdapter(myAdapter);
	        
	        registerForContextMenu(getListView());

	        getListView().setLongClickable(true);
	        
	    }
	    
	

	public void onCreateContextMenu(ContextMenu menu, View v,
		ContextMenuInfo menuInfo) {
		if (v.getId()==getListView().getId()){
			menu.setHeaderTitle(R.string.menu_title);
			menu.add(Menu.NONE, 0, 0, R.string.menu1);
			menu.add(Menu.NONE, 1, 1, R.string.menu2);
		}
	super.onCreateContextMenu(menu, v, menuInfo);
	}    
	    
	
	
	public boolean onContextItemSelected(MenuItem item) {
	  AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
	  int menuItemIndex = item.getItemId();
	  switch(menuItemIndex){
	  		case 0:
	  			callSelectedContact(info.position);
	  			break;
	  		case 1:
	  			sendMsgSelectedContact(info.position);
	  			break;
	  		default:
	  }
	  return true;
	}
	
	
	private void sendMsgSelectedContact(int position) {
		Intent intent = new Intent(Intent.ACTION_VIEW);
		Cursor cursor = (Cursor) myAdapter.getItem(position);
        long phoneId = cursor.getLong(cursor.getColumnIndex(People.NUMBER));
        
		intent.setData(Uri.parse("sms:" + phoneId));
		startActivity(intent);
	}


	protected void onListItemClick(ListView listView, View view, int position, long id) {
	    super.onListItemClick(listView, view, position, id);

	    callSelectedContact(position);
	}
	
	private void callSelectedContact(int position){
	    Intent intent = new Intent(Intent.ACTION_CALL);
        Cursor cursor = (Cursor) myAdapter.getItem(position);
        long phoneId = cursor.getLong(cursor.getColumnIndex(People.PRIMARY_PHONE_ID));
        intent.setData(ContentUris.withAppendedId(Phones.CONTENT_URI, phoneId));
        startActivity(intent);
	}
	
}




